#!/usr/bin/env bash

# ----------------------------------------------------------------------
# Filename:   ft-generalRequest.sh
# Version:    1.0
# Date:       2020/05/16
# Author:     Lz
# Email:      lz843723683@gmail.com
# History：     
#             Version 1.0, 2020/05/16
# Function:   用于定义和实现常用的需求
# ----------------------------------------------------------------------


## TODO ：默认开启acpid 服务,如果不存在apcid，存在systemd也行
#
AcpidEnable_FTGENREQ(){
        local svcname="acpid"

        # 判断是否存在服务
        SvcExist_FTLIB "isExist" "no" "${svcname}"
        if [ $? -ne 0 ];then
        # 不存在acpid服务
                local pkgname="systemd"
                # 判断存在systemd包
                PkgExist_FTLIB "isExist" "no" "yes" "$pkgname"
                RetParse_FTLIB "存在 ${pkgname} 软件包" "False"

                # 判断存在systemctl命令
                which systemctl
                RetParse_FTLIB "存在systemctl(不需要 ${svcname} 服务)" "False"
        else
        # 存在acpid服务
                # 判断服务是否激活
                SvcActive_FTLIB "isActive" "no" "${svcname}"
                RetParse_FTLIB "已启动${svcname}服务" "False"

                # 开机自启动
                SvcEnable_FTLIB "isEnable" "no" "${svcname}"
                RetParse_FTLIB "开机自启动${svcname}服务" "False"
        fi
}


## TODO ：默认集成mono，并且版本为5.4
#
Mono_FTGENREQ(){
        local pkgname="mono"
	local cmd=$pkgname

        # 测试默认集成mono
        PkgExist_FTLIB "isExist" "no" "no" "${pkgname}"
        RetParse_FTLIB "集成${pkgname}相关包" "False"

        # 判断版本号
	VersionEQTest_FTLIB "$cmd" "--version" "5.4"
}


## TODO ：默认集成java，并且版本为1.8.0_162
#
JavaVersion_FTGENREQ(){
	local cmd="java"
        # 判断版本号

	VersionEQTest_FTLIB "$cmd" "-version" "1.8.0_162"
}


## TODO ：1. 存在"node-v8.10.0-linux-x64"文件夹
#         2. 环境变量打印包含：/opt/goldwind/viewSoftware/node-v8.10.0-linux-x64/bin/
#
Node_FTGENREQ(){
        ls -ald /opt/goldwind/viewSoftware/node-v8.10.0-linux-x64
        RetParse_FTLIB "存在node-v8.10.0-linux-x64文件夹" "False"

## 目前在sysadm下使用sudo测试，打印的环境变量有问题，和普通用户打印不一致,所以先注释
#        local flag="/opt/goldwind/viewSoftware/node-v8.10.0-linux-x64/bin/"
#        echo "echo \$PATH"
#	echo "$PATH"
#        echo $PATH | grep "$flag"
#        if [ $? -eq 0 ];then
#                OutputRet_FTLIB "$TPASS"
#                # 存在环境变量
#                RetParse_FTLIB "环境变量打印包含：$flag" "False"
#        else
#                echo "不存在环境变量 $flag"
#                OutputRet_FTLIB "$TFAIL"
#                RetParse_FTLIB "环境变量打印包含：$flag" "False"
#        fi
}


## TODO ：1. /opt/goldwind/Agent目录下包含最新Agent.jar 
#         2. 开机自动运行
#
NewAgent_FTGENREQ(){
        # 测试/opt/goldwind/Agent目录下包含最新Agent.jar
        ls -ald /opt/goldwind/Agent/Agent.jar
        RetParse_FTLIB "/opt/goldwind/Agent目录下包含Agent.jar" "False"

        # 测试目前正在运行Agent.jar
        echo "ps -aux | grep -i agent.jar"
        ps -aux | grep -i agent.jar | grep "java -jar"
        if [ $? -eq 0 ];then
        # 存在agent.jar进程
                OutputRet_FTLIB "$TPASS"
                RetParse_FTLIB "目前正在运行Agent.jar" "False"
        else
        # 不存在agent.jar进程
                echo "未启动进程 agent.jar"
                OutputRet_FTLIB "$TFAIL"
                RetParse_FTLIB "目前正在运行Agent.jar" "False"
        fi

        # 测试Agent.jar已经添加到rc.local中
        echo "cat /etc/rc.local | grep \"java -jar /opt/goldwind/Agent/Agent.jar\""
        cat /etc/rc.local | grep "java -jar /opt/goldwind/Agent/Agent.jar"
        if [ $? -eq 0 ];then
        # 找到对应字段
                OutputRet_FTLIB "$TPASS"
                RetParse_FTLIB "已经添加到rc.local中" "False"
        else
        # 未找到对应字段
                echo "未在/etc/rc.local中添加java -jar /opt/goldwind/Agent/Agent.jar"
                OutputRet_FTLIB "$TFAIL"
                RetParse_FTLIB "已经添加到rc.local中" "False"
        fi
}


## TODO ：默认安装Ntp，并且服务能够正常启动
#
InstallNtp_FTGENREQ(){
        local pkgname="ntp"
        local svcname="ntpd"
        local ret="${TPASS}"

        # 默认安装ntp
        PkgExist_FTLIB "isExist" "no" "yes" "${pkgname}"
        RetParse_FTLIB "默认安装 ${pkgname} 相关包" "False"

        # 判断是否存在ntp
        SvcExist_FTLIB "isExist" "no" "${svcname}"
        RetParse_FTLIB "存在 ${svcname} 服务" "False"

        # 判断ntp是否默认打开
        SvcActive_FTLIB "isActive" "yes" "${svcname}"
        if [ $? -eq 0 ];then
        # 已经打开ntp
                SvcActive_FTLIB "isActive" "no" "${svcname}"
                RetParse_FTLIB "已启动${svcname}服务" "False"
        else
        # 当前ntp处于关闭状态
		which systemctl &>/dev/null
		if [ $? -eq 0 ];then
                	systemctl start ${svcname}
		else
			service ${svcname} start
		fi 

                SvcActive_FTLIB "isActive" "no" "${svcname}"
                RetParse_FTLIB "可以成功启动${svcname}服务" "False"

		which systemctl &>/dev/null
		if [ $? -eq 0 ];then
                	systemctl stop ${svcname}
		else
	                service ${svcname} stop
		fi 
        fi
}


export -f AcpidEnable_FTGENREQ
export -f Mono_FTGENREQ
export -f JavaVersion_FTGENREQ
export -f Node_FTGENREQ
export -f NewAgent_FTGENREQ
export -f InstallNtp_FTGENREQ
